package com.jianneng.jdg.model.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.com.jianneng.jdg.R;
import com.jianneng.jdg.model.homeactivity.ContributeActivity;

@SuppressLint("ResourceAsColor")
public class FragmentCircle extends Fragment implements OnClickListener{
	
	private TextView tv_square,tv_workfriend,tv_ask;
	private Fragment mSquare,mWorkfriend,mAsk;
	private Fragment currentFragment;
	private TextView circle_publish;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View view =inflater.inflate(R.layout.fragment_circle, container, false);
		tv_square=(TextView) view.findViewById(R.id.tv_square);
		tv_workfriend=(TextView) view.findViewById(R.id.tv_workfriend);
		tv_ask=(TextView) view.findViewById(R.id.tv_ask);
		circle_publish=(TextView) view.findViewById(R.id.tv_fragment_circle_publish);
		tv_square.setOnClickListener(this);
		tv_workfriend.setOnClickListener(this);
		tv_ask.setOnClickListener(this);
		circle_publish.setOnClickListener(this);
		return view;
	
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initChildFragment();
		
	}

	private void initChildFragment(){
		
		if (mSquare==null) {
			mSquare=new FragmentCircleSquare();
		}
		if (!mSquare.isAdded()) {
			
			getChildFragmentManager().beginTransaction().add(R.id.rl_fragment, mSquare).commit();
			currentFragment=mSquare;
		}else {
			getChildFragmentManager().beginTransaction().show(mSquare);
		}
		
	}
	private void showChildCircleFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment)
			return;
		if (!fragment.isAdded()) { // ���ǰfragmentδ����ӣ�����ӵ�Fragment��������
			transaction.hide(currentFragment).add(R.id.rl_fragment, fragment).commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}
		currentFragment = fragment;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
		case R.id.tv_square:
			if (mSquare==null) {
				mSquare=new FragmentCircleSquare();
			}
			showChildCircleFragment(getChildFragmentManager().beginTransaction(), mSquare);
			circle_publish.setVisibility(View.VISIBLE);
			tv_square.setBackgroundResource(R.drawable.textview_white_padding_border);
			tv_square.setTextColor(R.color.blue);
			tv_workfriend.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_workfriend.setTextColor(R.color.white);
			tv_ask.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_ask.setTextColor(R.color.white);
			
			break;
		case R.id.tv_workfriend:
			if (mWorkfriend==null) {
				mWorkfriend=new FragmentCircleWorkFriend();
			}
			showChildCircleFragment(getChildFragmentManager().beginTransaction(), mWorkfriend);
			circle_publish.setVisibility(View.VISIBLE);
			tv_square.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_square.setTextColor(R.color.white);
			tv_workfriend.setBackgroundResource(R.drawable.textview_white_padding_border);
			tv_workfriend.setTextColor(R.color.blue);
			tv_ask.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_ask.setTextColor(R.color.blue);
			break;

		case R.id.tv_ask:
			if (mAsk==null) {
				mAsk=new FragmentCircleAsk();
			}
			showChildCircleFragment(getChildFragmentManager().beginTransaction(), mAsk);
			circle_publish.setVisibility(View.INVISIBLE);
			tv_square.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_square.setTextColor(R.color.white);
			tv_workfriend.setBackgroundResource(R.drawable.textview_white_no_padding_border);
			tv_workfriend.setTextColor(R.color.white);
			tv_ask.setBackgroundResource(R.drawable.textview_white_padding_border);
			tv_ask.setTextColor(R.color.blue);
			break;
		case R.id.tv_fragment_circle_publish:
			Toast.makeText(getActivity(), "发布尼玛啊", 1).show();
			Intent intent =new Intent(getActivity(),ContributeActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}		
}
		
	
	
	
	
	

